package com.ly.mpayment.persist.service.uporderrefund;

import java.io.Serializable;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.ly.mpayment.persist.entity.UpOrderRefund;
import com.ly.mpayment.persist.hibernate.Page;
import com.ly.mpayment.persist.service.AbstractService;

@Service
public class UpOrderRefundServiceImpl extends AbstractService<UpOrderRefund, Serializable> implements IUpOrderRefundService {

	@Override
	public boolean addUporderrefund(UpOrderRefund fund) {
		if(fund != null){
			this.saveOrUpdate(fund);
			return true;
		}
		return false;
	}

	@Override
	public boolean updateUporderrefund(UpOrderRefund fund) {
		if(fund != null){
			this.saveOrUpdate(fund);
			return true;
		}
		return false;
	}

	@Override
	public UpOrderRefund findById(String id) {
		return this.findUnique("select u from UpOrderRefund u where u.uprefundstatus = 1 and u.uprefunduuid = id", id);
	}

	@Override
	public Page<UpOrderRefund> findPageUporderrefund(Map<String, Object> parameters, int start, int pageSize) {
		String hql = "select u from UpOrderRefund u left join fetch u.upOrder up left join fetch u.user us where 1 =1 " +
				"  #[and up.upphonenum = :phoneNum]#  #[and us.useruuid = :useruuid]#  #[and u.uprefundstatus = :uprefundstatus]# ";
		if (parameters.containsKey(":starttime")) {
			hql += " and u.uprefundtime >='" + parameters.get(":starttime").toString().concat(" 00:00:00'");
			parameters.remove(":starttime");
		}

		if (parameters.containsKey(":endtime")) {
			hql += " and u.uprefundtime <='" + parameters.get(":endtime").toString().concat(" 23:59:59'");
			parameters.remove(":endtime");
		}

		hql += "  order by u.uprefundtime desc ";
		return this.findPageByNamed(hql, parameters, start, pageSize);
	}

	@Override
	public UpOrderRefund findByUpOrderId(String upOrderId) {
		return this.findUnique("select u from UpOrderRefund u where u.upOrder.uporderuuid = ?", upOrderId);
	}

}
